package com.atguigu.srb.core.controller;


import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.common.exception.Assert;
import com.atguigu.srb.common.result.R;
import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.common.util.RegexValidateUtils;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.vo.LoginVO;
import com.atguigu.srb.core.pojo.vo.RegisterVO;
import com.atguigu.srb.core.pojo.vo.UserInfoVO;
import com.atguigu.srb.core.service.UserInfoService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author LiLi
 * @since 2021-09-23
 */
@RestController
@RequestMapping("/api/core/userInfo")
//@CrossOrigin
public class UserInfoController {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private UserInfoService userInfoService;

    @ApiOperation("校验手机号是否注册")
    @GetMapping("/getUserInfoById")
    public R getUserInfoById(HttpServletRequest request){
        String token = request.getHeader("token");
        Assert.notNull(token,ResponseEnum.WEIXIN_FETCH_USERINFO_ERROR);
        Long userId = JwtUtils.getUserId(token);
        UserInfo userInfo = userInfoService.getUserInfoById(userId);
        return R.ok().data("userInfo",userInfo);
    }

    @ApiOperation("校验手机号是否注册")
    @GetMapping("/checkMobile/{mobile}")
    public boolean checkMobile(@PathVariable String mobile){

        return userInfoService.checkMobile(mobile);
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public R login(@RequestBody LoginVO loginVO, HttpServletRequest request) {
        UserInfoVO userInfoVO = new UserInfoVO();
        String ip = request .getRemoteAddr();


        if(StringUtils.isEmpty(ip)||ip.equals("0:0:0:0:0:0:0:1")){
            ip = request.getHeader("X-forwarded-for");
        }
        userInfoVO = userInfoService.login(loginVO,ip); //login请求由nginx发送的，需要nginx转发请求信息
//        UserInfoVO userInfoVO1 = userInfoService.login(loginVO, ip);
        return R.ok().data("userInfo",userInfoVO);
    }
    @ApiOperation("用户注册")
    @PostMapping("/register")
    public R register(@RequestBody RegisterVO registerVO){


        String mobile = registerVO.getMobile();
        Assert.notNull(mobile,ResponseEnum.MOBILE_NULL_ERROR);

        //校验验证码是否正确

        boolean b = userInfoService.checkSmsCode(registerVO.getCode(),mobile);
        Assert.isTrue(b,ResponseEnum.CODE_ERROR);
//        String codeGen = (String) redisTemplate.opsForValue().get("srb:sms:code"+mobile);
//
//        System.out.println("code"+code);
//        System.out.println("codeGen"+codeGen);
//        Assert.equals(code,codeGen, ResponseEnum.CODE_ERROR);

        //注册
        userInfoService.register(registerVO);
        return R.ok();


    }
}

